package cn.edu.xmu.oomall.liquidation.dao;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.liquidation.mapper.ExpenditureItemPoMapper;
import cn.edu.xmu.oomall.liquidation.model.po.ExpenditureItemPo;
import cn.edu.xmu.oomall.liquidation.model.po.ExpenditureItemPoExample;
import cn.edu.xmu.oomall.liquidation.model.po.LiquidationPo;
import cn.edu.xmu.privilegegateway.annotation.util.Common;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author HertzLiu
 * @create_time 2021/12/8 23:25
 * @package_name cn.edu.xmu.oomall.liquidation.dao
 * @project_name oomall
 * @product_name IntelliJ IDEA
 * @description
 */

@Repository
public class ExpenditureDao {

    @Autowired
    private ExpenditureItemPoMapper expenditureItemPoMapper;

    public ReturnObject<PageInfo<ExpenditureItemPo>> getExpenditureListPageInfo(ExpenditureItemPoExample expenditureItemPoExample, Integer page, Integer pageSize) {
        try {
            PageHelper.startPage(page, pageSize);
            List<ExpenditureItemPo> expenditureItemPoList = expenditureItemPoMapper.selectByExample(expenditureItemPoExample);
            PageInfo<ExpenditureItemPo> expenditureItemPoListPageInfo = PageInfo.of(expenditureItemPoList);
            return new ReturnObject<>(expenditureItemPoListPageInfo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject<Object> getExpenditureById(Long shopId, Long id) {
        try {
            ExpenditureItemPo expenditureItemPo = expenditureItemPoMapper.selectByPrimaryKey(id);
            if (expenditureItemPo == null) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_NOTEXIST, "出账单id不存在");
            }
            if (!expenditureItemPo.getShopId().equals(shopId) && shopId != 0) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE, "该出账单不属于本商铺");
            }
            return new ReturnObject<>(expenditureItemPo);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject getExpenditureListByLiquidationId(ExpenditureItemPoExample expenditureItemPoExample) {
        try {
            List<ExpenditureItemPo> expenditureItemPoList = expenditureItemPoMapper.selectByExample(expenditureItemPoExample);
            if (expenditureItemPoList == null || expenditureItemPoList.size() == 0) {
                return new ReturnObject<>(ReturnNo.RESOURCE_ID_OUTSCOPE);
            }
            return new ReturnObject<>(expenditureItemPoList);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR);
        }
    }

    public ReturnObject insertExpenditureItemPo(ExpenditureItemPo expenditureItemPo, Long adminId, String adminName) {
        try {
            Common.setPoCreatedFields(expenditureItemPo, adminId, adminName);
            int ret = expenditureItemPoMapper.insertSelective(expenditureItemPo);
            if (ret == 0) {
                return new ReturnObject<>(ReturnNo.FIELD_NOTVALID,"insertExpenditureItemPo报错");
            }
            return new ReturnObject<>(ReturnNo.OK);
        } catch (Exception e) {
            return new ReturnObject<>(ReturnNo.INTERNAL_SERVER_ERR,"insertExpenditureItemPo报错");
        }
    }
}
